{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \"backtesting crypto with exit signal\"\n",
    "> \"How to fetch and backtest crypto data using fastquant\"\n",
    "\n",
    "- toc: true\n",
    "- branch: master\n",
    "- badges: true\n",
    "- comments: true\n",
    "- author: Mikee Jazmines\n",
    "- categories: [crypto, backtest]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/enzoampil/fastquant/blob/master/examples/2020-05-20-backtest_crypto.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# uncomment to install in colab\n",
    "# !pip3 install fastquant --update\n",
    "# or pip install git+https://www.github.com/enzoampil/fastquant.git@history"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## fetch data from binance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### If a timestamp is given, it will return upto that timestamp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fastquant import get_crypto_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "crypto = get_crypto_data(\"ADA/USDT\", \n",
    "                         \"2022-01-01 00:00:00\", \n",
    "                         \"2022-03-03 00:00:00\",\n",
    "                         time_resolution='1h'\n",
    "                        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dt</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-03-02 20:00:00</th>\n",
       "      <td>0.935</td>\n",
       "      <td>0.938</td>\n",
       "      <td>0.931</td>\n",
       "      <td>0.935</td>\n",
       "      <td>2939304.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-03-02 21:00:00</th>\n",
       "      <td>0.936</td>\n",
       "      <td>0.947</td>\n",
       "      <td>0.933</td>\n",
       "      <td>0.946</td>\n",
       "      <td>2943418.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-03-02 22:00:00</th>\n",
       "      <td>0.946</td>\n",
       "      <td>0.952</td>\n",
       "      <td>0.940</td>\n",
       "      <td>0.943</td>\n",
       "      <td>2918147.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-03-02 23:00:00</th>\n",
       "      <td>0.943</td>\n",
       "      <td>0.946</td>\n",
       "      <td>0.936</td>\n",
       "      <td>0.939</td>\n",
       "      <td>2451045.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-03-03 00:00:00</th>\n",
       "      <td>0.938</td>\n",
       "      <td>0.946</td>\n",
       "      <td>0.931</td>\n",
       "      <td>0.940</td>\n",
       "      <td>4678771.1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      open   high    low  close     volume\n",
       "dt                                                        \n",
       "2022-03-02 20:00:00  0.935  0.938  0.931  0.935  2939304.5\n",
       "2022-03-02 21:00:00  0.936  0.947  0.933  0.946  2943418.6\n",
       "2022-03-02 22:00:00  0.946  0.952  0.940  0.943  2918147.9\n",
       "2022-03-02 23:00:00  0.943  0.946  0.936  0.939  2451045.1\n",
       "2022-03-03 00:00:00  0.938  0.946  0.931  0.940  4678771.1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "crypto.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EMA Crossover Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fastquant import backtest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import modules\n",
    "import backtrader as bt\n",
    "\n",
    "# Import from package\n",
    "from fastquant.strategies.base import BaseStrategy\n",
    "\n",
    "\n",
    "class EMACStrategy(BaseStrategy):\n",
    "    \"\"\"\n",
    "    Exponential moving average crossover strategy\n",
    "\n",
    "    Parameters\n",
    "    ----------\n",
    "    fast_period : int\n",
    "        The period used for the fast exponential moving average line (should be smaller than `slow_upper`)\n",
    "    slow_period : int\n",
    "        The period used for the slow exponential moving average line (should be larger than `fast_upper`)\n",
    "\n",
    "    \"\"\"\n",
    "\n",
    "    params = (\n",
    "        (\"fast_period\", 10),  # period for the fast moving average\n",
    "        (\"slow_period\", 30),\n",
    "    )\n",
    "\n",
    "    def __init__(self):\n",
    "        # Initialize global variables\n",
    "        super().__init__()\n",
    "        # Strategy level variables\n",
    "        self.fast_period = self.params.fast_period\n",
    "        self.slow_period = self.params.slow_period\n",
    "        self.short_max = 1\n",
    "\n",
    "        if self.strategy_logging:\n",
    "            print(\"===Strategy level arguments===\")\n",
    "            print(\"fast_period :\", self.fast_period)\n",
    "            print(\"slow_period :\", self.slow_period)\n",
    "            print(\"allow_short :\", self.allow_short)\n",
    "        ema_fast = bt.ind.EMA(period=self.fast_period)  # fast moving average\n",
    "        ema_slow = bt.ind.EMA(period=self.slow_period)  # slow moving average\n",
    "        self.crossover = bt.ind.CrossOver(\n",
    "            ema_fast, ema_slow\n",
    "        )  # crossover signal\n",
    "\n",
    "    def buy_signal(self):\n",
    "        return self.crossover > 0\n",
    "\n",
    "    def sell_signal(self):\n",
    "        return self.crossover < 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Long only"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "results, history = backtest(EMACStrategy, \n",
    "                               crypto, \n",
    "                            fast_period = 20,\n",
    "                            slow_period = 50,\n",
    "                               plot=False,\n",
    "                               verbose=False,\n",
    "                               return_history=True,\n",
    "                            # allow_short=True,\n",
    "                              )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>strat_id</th>\n",
       "      <th>strat_name</th>\n",
       "      <th>dt</th>\n",
       "      <th>type</th>\n",
       "      <th>price</th>\n",
       "      <th>size</th>\n",
       "      <th>order_value</th>\n",
       "      <th>portfolio_value</th>\n",
       "      <th>commission</th>\n",
       "      <th>pnl</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-05 11:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.345</td>\n",
       "      <td>74275</td>\n",
       "      <td>99899.875</td>\n",
       "      <td>98737.325</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-05 13:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.329</td>\n",
       "      <td>-74275</td>\n",
       "      <td>99899.875</td>\n",
       "      <td>98811.600</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1188.400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-11 20:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.187</td>\n",
       "      <td>83161</td>\n",
       "      <td>98712.107</td>\n",
       "      <td>98894.761</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-19 06:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.409</td>\n",
       "      <td>-83161</td>\n",
       "      <td>98712.107</td>\n",
       "      <td>117273.342</td>\n",
       "      <td>0.0</td>\n",
       "      <td>18461.742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-26 13:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.096</td>\n",
       "      <td>106894</td>\n",
       "      <td>117155.824</td>\n",
       "      <td>118662.964</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-27 05:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.024</td>\n",
       "      <td>-106894</td>\n",
       "      <td>117155.824</td>\n",
       "      <td>109576.974</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-7696.368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-29 07:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.057</td>\n",
       "      <td>103564</td>\n",
       "      <td>109467.148</td>\n",
       "      <td>111233.998</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-30 20:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.031</td>\n",
       "      <td>-103564</td>\n",
       "      <td>109467.148</td>\n",
       "      <td>106884.310</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-2692.664</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 04:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.056</td>\n",
       "      <td>101115</td>\n",
       "      <td>106777.440</td>\n",
       "      <td>106783.195</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-02 22:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.032</td>\n",
       "      <td>-101115</td>\n",
       "      <td>106777.440</td>\n",
       "      <td>104457.550</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-2426.760</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-04 06:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.068</td>\n",
       "      <td>97708</td>\n",
       "      <td>104352.144</td>\n",
       "      <td>104555.258</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-09 04:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.153</td>\n",
       "      <td>-97708</td>\n",
       "      <td>104352.144</td>\n",
       "      <td>112762.730</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8305.180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-09 08:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.186</td>\n",
       "      <td>94983</td>\n",
       "      <td>112649.838</td>\n",
       "      <td>112192.832</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-10 22:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.162</td>\n",
       "      <td>-94983</td>\n",
       "      <td>112649.838</td>\n",
       "      <td>110483.138</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-2279.592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-15 07:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.069</td>\n",
       "      <td>103248</td>\n",
       "      <td>110372.112</td>\n",
       "      <td>110689.634</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-17 10:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.068</td>\n",
       "      <td>-103248</td>\n",
       "      <td>110372.112</td>\n",
       "      <td>110379.890</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-103.248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-23 12:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>0.930</td>\n",
       "      <td>118569</td>\n",
       "      <td>110269.170</td>\n",
       "      <td>112158.425</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-24 00:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>0.866</td>\n",
       "      <td>-118569</td>\n",
       "      <td>110269.170</td>\n",
       "      <td>102791.474</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-7588.416</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-25 16:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>0.883</td>\n",
       "      <td>116295</td>\n",
       "      <td>102688.485</td>\n",
       "      <td>102907.769</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-27 21:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>0.851</td>\n",
       "      <td>-116295</td>\n",
       "      <td>102688.485</td>\n",
       "      <td>99070.034</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-3721.440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-28 15:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>0.914</td>\n",
       "      <td>108283</td>\n",
       "      <td>98970.662</td>\n",
       "      <td>101885.392</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    strat_id                   strat_name                  dt  type  price  \\\n",
       "0          0  fast_period20_slow_period50 2022-01-05 11:00:00   buy  1.345   \n",
       "1          0  fast_period20_slow_period50 2022-01-05 13:00:00  sell  1.329   \n",
       "2          0  fast_period20_slow_period50 2022-01-11 20:00:00   buy  1.187   \n",
       "3          0  fast_period20_slow_period50 2022-01-19 06:00:00  sell  1.409   \n",
       "4          0  fast_period20_slow_period50 2022-01-26 13:00:00   buy  1.096   \n",
       "5          0  fast_period20_slow_period50 2022-01-27 05:00:00  sell  1.024   \n",
       "6          0  fast_period20_slow_period50 2022-01-29 07:00:00   buy  1.057   \n",
       "7          0  fast_period20_slow_period50 2022-01-30 20:00:00  sell  1.031   \n",
       "8          0  fast_period20_slow_period50 2022-02-01 04:00:00   buy  1.056   \n",
       "9          0  fast_period20_slow_period50 2022-02-02 22:00:00  sell  1.032   \n",
       "10         0  fast_period20_slow_period50 2022-02-04 06:00:00   buy  1.068   \n",
       "11         0  fast_period20_slow_period50 2022-02-09 04:00:00  sell  1.153   \n",
       "12         0  fast_period20_slow_period50 2022-02-09 08:00:00   buy  1.186   \n",
       "13         0  fast_period20_slow_period50 2022-02-10 22:00:00  sell  1.162   \n",
       "14         0  fast_period20_slow_period50 2022-02-15 07:00:00   buy  1.069   \n",
       "15         0  fast_period20_slow_period50 2022-02-17 10:00:00  sell  1.068   \n",
       "16         0  fast_period20_slow_period50 2022-02-23 12:00:00   buy  0.930   \n",
       "17         0  fast_period20_slow_period50 2022-02-24 00:00:00  sell  0.866   \n",
       "18         0  fast_period20_slow_period50 2022-02-25 16:00:00   buy  0.883   \n",
       "19         0  fast_period20_slow_period50 2022-02-27 21:00:00  sell  0.851   \n",
       "20         0  fast_period20_slow_period50 2022-02-28 15:00:00   buy  0.914   \n",
       "\n",
       "      size  order_value  portfolio_value  commission        pnl  \n",
       "0    74275    99899.875        98737.325         0.0      0.000  \n",
       "1   -74275    99899.875        98811.600         0.0  -1188.400  \n",
       "2    83161    98712.107        98894.761         0.0      0.000  \n",
       "3   -83161    98712.107       117273.342         0.0  18461.742  \n",
       "4   106894   117155.824       118662.964         0.0      0.000  \n",
       "5  -106894   117155.824       109576.974         0.0  -7696.368  \n",
       "6   103564   109467.148       111233.998         0.0      0.000  \n",
       "7  -103564   109467.148       106884.310         0.0  -2692.664  \n",
       "8   101115   106777.440       106783.195         0.0      0.000  \n",
       "9  -101115   106777.440       104457.550         0.0  -2426.760  \n",
       "10   97708   104352.144       104555.258         0.0      0.000  \n",
       "11  -97708   104352.144       112762.730         0.0   8305.180  \n",
       "12   94983   112649.838       112192.832         0.0      0.000  \n",
       "13  -94983   112649.838       110483.138         0.0  -2279.592  \n",
       "14  103248   110372.112       110689.634         0.0      0.000  \n",
       "15 -103248   110372.112       110379.890         0.0   -103.248  \n",
       "16  118569   110269.170       112158.425         0.0      0.000  \n",
       "17 -118569   110269.170       102791.474         0.0  -7588.416  \n",
       "18  116295   102688.485       102907.769         0.0      0.000  \n",
       "19 -116295   102688.485        99070.034         0.0  -3721.440  \n",
       "20  108283    98970.662       101885.392         0.0      0.000  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orders = history['orders']\n",
    "orders"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The final value in `results` can be calculated from the `commission` and `pnl` (profit & loss) of all the closed (bought and sold) transactions in history:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "101885.39199999996"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = results.squeeze()\n",
    "r.final_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Strategy\n",
      "Profit of asset: 1.8853919999999635\n"
     ]
    }
   ],
   "source": [
    "profit = (r.final_value - 100000)/100000*100\n",
    "print(\"Strategy\")\n",
    "print(f\"Profit of asset: {profit}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Buy and hold\n",
      "Start price of asset: 1.334\n",
      "End price of asset: 0.94\n",
      "Profit of asset: -29.5352323838081\n"
     ]
    }
   ],
   "source": [
    "print(\"Buy and hold\")\n",
    "print(f\"Start price of asset: {crypto.iloc[0, 3]}\")\n",
    "print(f\"End price of asset: {crypto.iloc[-1, 3]}\")\n",
    "print(f\"Profit of asset: {(crypto.iloc[-1, 3] - crypto.iloc[0, 3])/crypto.iloc[0, 3]*100}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Long and Short"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "results, history = backtest(EMACStrategy, \n",
    "                               crypto, \n",
    "                            fast_period = 20,\n",
    "                            slow_period = 50,\n",
    "                               plot=False,\n",
    "                               verbose=False,\n",
    "                               return_history=True,\n",
    "                             allow_short=True,\n",
    "                              )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>strat_id</th>\n",
       "      <th>strat_name</th>\n",
       "      <th>dt</th>\n",
       "      <th>type</th>\n",
       "      <th>price</th>\n",
       "      <th>size</th>\n",
       "      <th>order_value</th>\n",
       "      <th>portfolio_value</th>\n",
       "      <th>commission</th>\n",
       "      <th>pnl</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-03 22:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.324</td>\n",
       "      <td>-75585</td>\n",
       "      <td>-100074.540</td>\n",
       "      <td>100075.585</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-05 11:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.345</td>\n",
       "      <td>148605</td>\n",
       "      <td>-1862.640</td>\n",
       "      <td>97171.375</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1587.285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-05 13:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.329</td>\n",
       "      <td>-146411</td>\n",
       "      <td>675.261</td>\n",
       "      <td>97537.959</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1168.320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-11 20:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.187</td>\n",
       "      <td>163931</td>\n",
       "      <td>9934.341</td>\n",
       "      <td>107756.457</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10421.522</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-19 06:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.409</td>\n",
       "      <td>-182325</td>\n",
       "      <td>-21854.085</td>\n",
       "      <td>129326.142</td>\n",
       "      <td>0.0</td>\n",
       "      <td>20099.880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-26 13:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.096</td>\n",
       "      <td>234337</td>\n",
       "      <td>26911.927</td>\n",
       "      <td>158347.678</td>\n",
       "      <td>0.0</td>\n",
       "      <td>28728.705</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-27 05:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.024</td>\n",
       "      <td>-285216</td>\n",
       "      <td>10149.056</td>\n",
       "      <td>146088.094</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-10263.744</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-29 07:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.057</td>\n",
       "      <td>276278</td>\n",
       "      <td>-4857.938</td>\n",
       "      <td>143660.670</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-4707.912</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-30 20:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.031</td>\n",
       "      <td>-266609</td>\n",
       "      <td>4112.153</td>\n",
       "      <td>137117.917</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-3473.964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 04:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.056</td>\n",
       "      <td>260314</td>\n",
       "      <td>-2668.981</td>\n",
       "      <td>134596.688</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-3324.875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-02 22:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.032</td>\n",
       "      <td>-254657</td>\n",
       "      <td>3036.048</td>\n",
       "      <td>131413.675</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-3055.656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-04 06:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.068</td>\n",
       "      <td>246084</td>\n",
       "      <td>-4592.088</td>\n",
       "      <td>127202.929</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-4584.168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-09 04:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.153</td>\n",
       "      <td>-236092</td>\n",
       "      <td>-8479.210</td>\n",
       "      <td>135300.057</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10093.410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-09 08:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.186</td>\n",
       "      <td>229515</td>\n",
       "      <td>-2267.504</td>\n",
       "      <td>132632.161</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-3872.418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-10 22:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.162</td>\n",
       "      <td>-223804</td>\n",
       "      <td>3312.564</td>\n",
       "      <td>129720.039</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-2692.056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-15 07:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>1.069</td>\n",
       "      <td>243286</td>\n",
       "      <td>11015.049</td>\n",
       "      <td>141258.476</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10382.055</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-17 10:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>1.068</td>\n",
       "      <td>-263793</td>\n",
       "      <td>-392.737</td>\n",
       "      <td>141127.807</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-131.651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-23 12:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>0.930</td>\n",
       "      <td>302913</td>\n",
       "      <td>17689.374</td>\n",
       "      <td>161660.684</td>\n",
       "      <td>0.0</td>\n",
       "      <td>18235.596</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-24 00:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>0.866</td>\n",
       "      <td>-343664</td>\n",
       "      <td>9091.692</td>\n",
       "      <td>149725.812</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-10929.344</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-25 16:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>0.883</td>\n",
       "      <td>337030</td>\n",
       "      <td>-4792.367</td>\n",
       "      <td>145394.731</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-2939.181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-27 21:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>0.851</td>\n",
       "      <td>-329400</td>\n",
       "      <td>4294.158</td>\n",
       "      <td>140639.262</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-5252.384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-28 15:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>0.914</td>\n",
       "      <td>306714</td>\n",
       "      <td>-11352.599</td>\n",
       "      <td>133244.367</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-10411.569</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    strat_id                   strat_name                  dt  type  price  \\\n",
       "0          0  fast_period20_slow_period50 2022-01-03 22:00:00  sell  1.324   \n",
       "1          0  fast_period20_slow_period50 2022-01-05 11:00:00   buy  1.345   \n",
       "2          0  fast_period20_slow_period50 2022-01-05 13:00:00  sell  1.329   \n",
       "3          0  fast_period20_slow_period50 2022-01-11 20:00:00   buy  1.187   \n",
       "4          0  fast_period20_slow_period50 2022-01-19 06:00:00  sell  1.409   \n",
       "5          0  fast_period20_slow_period50 2022-01-26 13:00:00   buy  1.096   \n",
       "6          0  fast_period20_slow_period50 2022-01-27 05:00:00  sell  1.024   \n",
       "7          0  fast_period20_slow_period50 2022-01-29 07:00:00   buy  1.057   \n",
       "8          0  fast_period20_slow_period50 2022-01-30 20:00:00  sell  1.031   \n",
       "9          0  fast_period20_slow_period50 2022-02-01 04:00:00   buy  1.056   \n",
       "10         0  fast_period20_slow_period50 2022-02-02 22:00:00  sell  1.032   \n",
       "11         0  fast_period20_slow_period50 2022-02-04 06:00:00   buy  1.068   \n",
       "12         0  fast_period20_slow_period50 2022-02-09 04:00:00  sell  1.153   \n",
       "13         0  fast_period20_slow_period50 2022-02-09 08:00:00   buy  1.186   \n",
       "14         0  fast_period20_slow_period50 2022-02-10 22:00:00  sell  1.162   \n",
       "15         0  fast_period20_slow_period50 2022-02-15 07:00:00   buy  1.069   \n",
       "16         0  fast_period20_slow_period50 2022-02-17 10:00:00  sell  1.068   \n",
       "17         0  fast_period20_slow_period50 2022-02-23 12:00:00   buy  0.930   \n",
       "18         0  fast_period20_slow_period50 2022-02-24 00:00:00  sell  0.866   \n",
       "19         0  fast_period20_slow_period50 2022-02-25 16:00:00   buy  0.883   \n",
       "20         0  fast_period20_slow_period50 2022-02-27 21:00:00  sell  0.851   \n",
       "21         0  fast_period20_slow_period50 2022-02-28 15:00:00   buy  0.914   \n",
       "\n",
       "      size  order_value  portfolio_value  commission        pnl  \n",
       "0   -75585  -100074.540       100075.585         0.0      0.000  \n",
       "1   148605    -1862.640        97171.375         0.0  -1587.285  \n",
       "2  -146411      675.261        97537.959         0.0  -1168.320  \n",
       "3   163931     9934.341       107756.457         0.0  10421.522  \n",
       "4  -182325   -21854.085       129326.142         0.0  20099.880  \n",
       "5   234337    26911.927       158347.678         0.0  28728.705  \n",
       "6  -285216    10149.056       146088.094         0.0 -10263.744  \n",
       "7   276278    -4857.938       143660.670         0.0  -4707.912  \n",
       "8  -266609     4112.153       137117.917         0.0  -3473.964  \n",
       "9   260314    -2668.981       134596.688         0.0  -3324.875  \n",
       "10 -254657     3036.048       131413.675         0.0  -3055.656  \n",
       "11  246084    -4592.088       127202.929         0.0  -4584.168  \n",
       "12 -236092    -8479.210       135300.057         0.0  10093.410  \n",
       "13  229515    -2267.504       132632.161         0.0  -3872.418  \n",
       "14 -223804     3312.564       129720.039         0.0  -2692.056  \n",
       "15  243286    11015.049       141258.476         0.0  10382.055  \n",
       "16 -263793     -392.737       141127.807         0.0   -131.651  \n",
       "17  302913    17689.374       161660.684         0.0  18235.596  \n",
       "18 -343664     9091.692       149725.812         0.0 -10929.344  \n",
       "19  337030    -4792.367       145394.731         0.0  -2939.181  \n",
       "20 -329400     4294.158       140639.262         0.0  -5252.384  \n",
       "21  306714   -11352.599       133244.367         0.0 -10411.569  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orders = history['orders']\n",
    "orders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>strat_id</th>\n",
       "      <th>strat_name</th>\n",
       "      <th>dt</th>\n",
       "      <th>portfolio_value</th>\n",
       "      <th>cash</th>\n",
       "      <th>size</th>\n",
       "      <th>return</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>673</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-30 19:00:00</td>\n",
       "      <td>138048.882</td>\n",
       "      <td>292.848</td>\n",
       "      <td>133614</td>\n",
       "      <td>-0.013369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>674</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-30 20:00:00</td>\n",
       "      <td>137117.917</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>-0.006744</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>675</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-30 21:00:00</td>\n",
       "      <td>137649.897</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>0.003880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>676</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-30 22:00:00</td>\n",
       "      <td>137383.907</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>-0.001932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>677</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-30 23:00:00</td>\n",
       "      <td>137516.902</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>0.000968</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>678</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 00:00:00</td>\n",
       "      <td>140043.807</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>0.018375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>679</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 01:00:00</td>\n",
       "      <td>140043.807</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>680</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 02:00:00</td>\n",
       "      <td>138979.847</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>-0.007597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>681</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 03:00:00</td>\n",
       "      <td>139245.837</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>0.001914</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>682</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 04:00:00</td>\n",
       "      <td>140442.792</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>0.008596</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>683</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 05:00:00</td>\n",
       "      <td>139777.817</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>-0.004735</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>684</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 06:00:00</td>\n",
       "      <td>140043.807</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>0.001903</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>685</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 07:00:00</td>\n",
       "      <td>139112.842</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>-0.006648</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>686</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 08:00:00</td>\n",
       "      <td>139112.842</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>687</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 09:00:00</td>\n",
       "      <td>139245.837</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>0.000956</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 10:00:00</td>\n",
       "      <td>138979.847</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>-0.001910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>689</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 11:00:00</td>\n",
       "      <td>138979.847</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>690</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 12:00:00</td>\n",
       "      <td>139644.822</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>0.004785</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>691</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 13:00:00</td>\n",
       "      <td>139245.837</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>-0.002857</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>692</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-01-31 14:00:00</td>\n",
       "      <td>139112.842</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>-0.000955</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     strat_id                   strat_name                  dt  \\\n",
       "673         0  fast_period20_slow_period50 2022-01-30 19:00:00   \n",
       "674         0  fast_period20_slow_period50 2022-01-30 20:00:00   \n",
       "675         0  fast_period20_slow_period50 2022-01-30 21:00:00   \n",
       "676         0  fast_period20_slow_period50 2022-01-30 22:00:00   \n",
       "677         0  fast_period20_slow_period50 2022-01-30 23:00:00   \n",
       "678         0  fast_period20_slow_period50 2022-01-31 00:00:00   \n",
       "679         0  fast_period20_slow_period50 2022-01-31 01:00:00   \n",
       "680         0  fast_period20_slow_period50 2022-01-31 02:00:00   \n",
       "681         0  fast_period20_slow_period50 2022-01-31 03:00:00   \n",
       "682         0  fast_period20_slow_period50 2022-01-31 04:00:00   \n",
       "683         0  fast_period20_slow_period50 2022-01-31 05:00:00   \n",
       "684         0  fast_period20_slow_period50 2022-01-31 06:00:00   \n",
       "685         0  fast_period20_slow_period50 2022-01-31 07:00:00   \n",
       "686         0  fast_period20_slow_period50 2022-01-31 08:00:00   \n",
       "687         0  fast_period20_slow_period50 2022-01-31 09:00:00   \n",
       "688         0  fast_period20_slow_period50 2022-01-31 10:00:00   \n",
       "689         0  fast_period20_slow_period50 2022-01-31 11:00:00   \n",
       "690         0  fast_period20_slow_period50 2022-01-31 12:00:00   \n",
       "691         0  fast_period20_slow_period50 2022-01-31 13:00:00   \n",
       "692         0  fast_period20_slow_period50 2022-01-31 14:00:00   \n",
       "\n",
       "     portfolio_value        cash    size    return  \n",
       "673       138048.882     292.848  133614 -0.013369  \n",
       "674       137117.917  275166.727 -132995 -0.006744  \n",
       "675       137649.897  275166.727 -132995  0.003880  \n",
       "676       137383.907  275166.727 -132995 -0.001932  \n",
       "677       137516.902  275166.727 -132995  0.000968  \n",
       "678       140043.807  275166.727 -132995  0.018375  \n",
       "679       140043.807  275166.727 -132995  0.000000  \n",
       "680       138979.847  275166.727 -132995 -0.007597  \n",
       "681       139245.837  275166.727 -132995  0.001914  \n",
       "682       140442.792  275166.727 -132995  0.008596  \n",
       "683       139777.817  275166.727 -132995 -0.004735  \n",
       "684       140043.807  275166.727 -132995  0.001903  \n",
       "685       139112.842  275166.727 -132995 -0.006648  \n",
       "686       139112.842  275166.727 -132995  0.000000  \n",
       "687       139245.837  275166.727 -132995  0.000956  \n",
       "688       138979.847  275166.727 -132995 -0.001910  \n",
       "689       138979.847  275166.727 -132995  0.000000  \n",
       "690       139644.822  275166.727 -132995  0.004785  \n",
       "691       139245.837  275166.727 -132995 -0.002857  \n",
       "692       139112.842  275166.727 -132995 -0.000955  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "periodic = history['periodic']\n",
    "periodic.loc[periodic['dt']>='2022-01-30 19:00:00'].head(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>strat_id</th>\n",
       "      <th>strat_name</th>\n",
       "      <th>dt</th>\n",
       "      <th>portfolio_value</th>\n",
       "      <th>cash</th>\n",
       "      <th>size</th>\n",
       "      <th>return</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>705</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 03:00:00</td>\n",
       "      <td>134724.007</td>\n",
       "      <td>275166.727</td>\n",
       "      <td>-132995</td>\n",
       "      <td>-0.010742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>706</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 04:00:00</td>\n",
       "      <td>134596.688</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>-0.000945</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>707</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 05:00:00</td>\n",
       "      <td>134342.050</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>-0.001892</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>708</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 06:00:00</td>\n",
       "      <td>133832.774</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>-0.003791</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>709</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 07:00:00</td>\n",
       "      <td>134724.007</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>0.006659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>710</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 08:00:00</td>\n",
       "      <td>135487.921</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>0.005670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>711</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 09:00:00</td>\n",
       "      <td>133960.093</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>-0.011276</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>712</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 10:00:00</td>\n",
       "      <td>133960.093</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>713</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 11:00:00</td>\n",
       "      <td>134596.688</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>0.004752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>714</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 12:00:00</td>\n",
       "      <td>135615.240</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>0.007567</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>715</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 13:00:00</td>\n",
       "      <td>135615.240</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>716</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 14:00:00</td>\n",
       "      <td>135233.283</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>-0.002816</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>717</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 15:00:00</td>\n",
       "      <td>135869.878</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>0.004707</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>718</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 16:00:00</td>\n",
       "      <td>137270.387</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>0.010308</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>719</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 17:00:00</td>\n",
       "      <td>136506.473</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>-0.005565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>720</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 18:00:00</td>\n",
       "      <td>136379.154</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>-0.000933</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>721</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 19:00:00</td>\n",
       "      <td>135869.878</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>-0.003734</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>722</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 20:00:00</td>\n",
       "      <td>136379.154</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>0.003748</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>723</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 21:00:00</td>\n",
       "      <td>137652.344</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>0.009336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>724</th>\n",
       "      <td>0</td>\n",
       "      <td>fast_period20_slow_period50</td>\n",
       "      <td>2022-02-01 22:00:00</td>\n",
       "      <td>137143.068</td>\n",
       "      <td>275.143</td>\n",
       "      <td>127319</td>\n",
       "      <td>-0.003700</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     strat_id                   strat_name                  dt  \\\n",
       "705         0  fast_period20_slow_period50 2022-02-01 03:00:00   \n",
       "706         0  fast_period20_slow_period50 2022-02-01 04:00:00   \n",
       "707         0  fast_period20_slow_period50 2022-02-01 05:00:00   \n",
       "708         0  fast_period20_slow_period50 2022-02-01 06:00:00   \n",
       "709         0  fast_period20_slow_period50 2022-02-01 07:00:00   \n",
       "710         0  fast_period20_slow_period50 2022-02-01 08:00:00   \n",
       "711         0  fast_period20_slow_period50 2022-02-01 09:00:00   \n",
       "712         0  fast_period20_slow_period50 2022-02-01 10:00:00   \n",
       "713         0  fast_period20_slow_period50 2022-02-01 11:00:00   \n",
       "714         0  fast_period20_slow_period50 2022-02-01 12:00:00   \n",
       "715         0  fast_period20_slow_period50 2022-02-01 13:00:00   \n",
       "716         0  fast_period20_slow_period50 2022-02-01 14:00:00   \n",
       "717         0  fast_period20_slow_period50 2022-02-01 15:00:00   \n",
       "718         0  fast_period20_slow_period50 2022-02-01 16:00:00   \n",
       "719         0  fast_period20_slow_period50 2022-02-01 17:00:00   \n",
       "720         0  fast_period20_slow_period50 2022-02-01 18:00:00   \n",
       "721         0  fast_period20_slow_period50 2022-02-01 19:00:00   \n",
       "722         0  fast_period20_slow_period50 2022-02-01 20:00:00   \n",
       "723         0  fast_period20_slow_period50 2022-02-01 21:00:00   \n",
       "724         0  fast_period20_slow_period50 2022-02-01 22:00:00   \n",
       "\n",
       "     portfolio_value        cash    size    return  \n",
       "705       134724.007  275166.727 -132995 -0.010742  \n",
       "706       134596.688     275.143  127319 -0.000945  \n",
       "707       134342.050     275.143  127319 -0.001892  \n",
       "708       133832.774     275.143  127319 -0.003791  \n",
       "709       134724.007     275.143  127319  0.006659  \n",
       "710       135487.921     275.143  127319  0.005670  \n",
       "711       133960.093     275.143  127319 -0.011276  \n",
       "712       133960.093     275.143  127319  0.000000  \n",
       "713       134596.688     275.143  127319  0.004752  \n",
       "714       135615.240     275.143  127319  0.007567  \n",
       "715       135615.240     275.143  127319  0.000000  \n",
       "716       135233.283     275.143  127319 -0.002816  \n",
       "717       135869.878     275.143  127319  0.004707  \n",
       "718       137270.387     275.143  127319  0.010308  \n",
       "719       136506.473     275.143  127319 -0.005565  \n",
       "720       136379.154     275.143  127319 -0.000933  \n",
       "721       135869.878     275.143  127319 -0.003734  \n",
       "722       136379.154     275.143  127319  0.003748  \n",
       "723       137652.344     275.143  127319  0.009336  \n",
       "724       137143.068     275.143  127319 -0.003700  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "periodic.loc[periodic['dt']>='2022-02-01 03:00:00'].head(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The final value in `results` can be calculated from the `commission` and `pnl` (profit & loss) of all the closed (bought and sold) transactions in history:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "133244.36700000003"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = results.squeeze()\n",
    "r.final_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Strategy\n",
      "Profit of asset: 33.24436700000003\n"
     ]
    }
   ],
   "source": [
    "profit2 = (r.final_value - 100000)/100000*100\n",
    "print(\"Strategy\")\n",
    "print(f\"Profit of asset: {profit2}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Buy and hold\n",
      "Start price of asset: 1.334\n",
      "End price of asset: 0.94\n",
      "Profit of asset: -29.5352323838081\n"
     ]
    }
   ],
   "source": [
    "print(\"Buy and hold\")\n",
    "print(f\"Start price of asset: {crypto.iloc[0, 3]}\")\n",
    "print(f\"End price of asset: {crypto.iloc[-1, 3]}\")\n",
    "print(f\"Profit of asset: {(crypto.iloc[-1, 3] - crypto.iloc[0, 3])/crypto.iloc[0, 3]*100}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Final Results:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Buy and hold profit %: -29.54%\n",
      "Long only profit %: 1.89%\n",
      "Long and Short profit %: 33.24%\n"
     ]
    }
   ],
   "source": [
    "print(f\"Buy and hold profit %: {round((crypto.iloc[-1, 3] - crypto.iloc[0, 3])/crypto.iloc[0, 3]*100, 2)}%\")\n",
    "print(f\"Long only profit %: {round(profit, 2)}%\")\n",
    "print(f\"Long and Short profit %: {round(profit2, 2)}%\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fq",
   "language": "python",
   "name": "fq"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
